package com.dawn.kingbase.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dawn.kingbase.dao.user.UserMapper;
import com.dawn.kingbase.model.page.PageWrap;
import com.dawn.kingbase.model.response.ApiResponse;
import com.dawn.kingbase.model.user.SysUser;
import com.dawn.kingbase.service.user.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户信息Service实现
 *
 * @author dongxiaoqi
 * @since 2022/12/19 16:33
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, SysUser> implements UserService {

    @Override
    public ApiResponse create(SysUser user) {
        boolean saveOrUpdate = this.saveOrUpdate(user);
        return ApiResponse.success(saveOrUpdate);
    }

    @Override
    public Boolean deleteById(String id) {
        return this.deleteById(id);
    }

    @Override
    public Boolean deleteByIdInBatch(List<Long> idList) {
        return this.deleteByIdInBatch(idList);
    }

    @Override
    public Boolean updateUserById(SysUser user) {
        int updateRows = this.baseMapper.updateById(user);
        return 1 == updateRows;
    }

    @Override
    public ApiResponse findById(String id) {
        return ApiResponse.success(this.baseMapper.selectById(id));
    }

    @Override
    public ApiResponse findUserById(String id) {
        return ApiResponse.success(this.baseMapper.findUserById(id));
    }

    @Override
    public IPage findPage(PageWrap<SysUser> pageWrap) {
        //设置查询条件
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.lambda().
                eq(ObjectUtils.isNotEmpty(pageWrap.getModel().getUserName()), SysUser::getUserName, pageWrap.getModel().getUserName())
                .eq(StringUtils.isNotBlank(pageWrap.getModel().getEmail()), SysUser::getEmail, pageWrap.getModel().getEmail())
                .eq(StringUtils.isNotBlank(pageWrap.getModel().getPhone()), SysUser::getPhone, pageWrap.getModel().getPhone())
                .eq(ObjectUtils.isNotEmpty(pageWrap.getModel().getSex()), SysUser::getSex, pageWrap.getModel().getSex());
        //设置分页参数
        IPage<SysUser> iPage = new Page<>(pageWrap.getPageNum(), pageWrap.getPageSize());
        //分页查询
        IPage<SysUser> page = baseMapper.selectPage(iPage, wrapper);
        return page;
    }
}
